/**
 * Created by huhai on 17/8/29.
 */
let express =require('express');
let app =express();
let path = require('path');
let fs = require('fs')

app.use(express.static(__dirname + '/resource'));


app.listen(3000,'0.0.0.0', function () {
  console.log('Example app listening on port 3000!');
});
// 获取所有item的信息
app.get("/itemList",function(req,res){
  let imageMap=[]
    // 读取所有images信息
  let imgUrl = './resource/images'
  var files = fs.readdirSync(imgUrl);
  files.forEach(function (filename) {
    let fullname = path.join(imgUrl,filename);
    let stats = fs.statSync(fullname);
    if (!stats.isDirectory()){
        //检测是否有同样名称的video
      let extractFileName = filename.replace(/(.*)\.(.*)/g, '$1')
      if(extractFileName){
        let fileNameNoDot = extractFileName
        let fileAddress = './resource/videos/'+fileNameNoDot+".mp4";
        console.log(fileAddress)
          if(fs.existsSync(fileAddress)){
            imageMap.push(extractFileName)
          }

      }

    }

  });
    res.set("Content-Type","application/json")

  let result = [];
  for(let i=0,len=imageMap.length;i<len;i+=4){
    result.push(imageMap.slice(i,i+4));
  }
    res.json({data: result})
})